package com.ruoyi.management.order.service.impl;

import com.ruoyi.adrss.Http;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.management.carservicestatics.mapper.TCarServiceCollectMapper;
import com.ruoyi.management.carservicestatics.service.ITCarServiceCollectService;
import com.ruoyi.management.carservicestatics.vo.SeleteCarServiceCollectByIdVo;
import com.ruoyi.management.order.mapper.DriverOrderManagementMapper;
import com.ruoyi.management.order.ro.*;
import com.ruoyi.management.order.service.DriverOrderManagementService;
import com.ruoyi.management.order.vo.*;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单
 * 
 * @author ruoyi
 * @date 2021-12-07
 */
@Service
public class DriverOrderManagementManagementServiceImpl implements DriverOrderManagementService
{
    @Autowired
    private DriverOrderManagementMapper driverOrderManagementMapper;
    @Autowired
    private TCarServiceCollectMapper carServiceCollectMapper;
    @Autowired
    private ISysConfigService configService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GetDriverOrderManagementListVo> getOrderList(GetDriverOrderManagementListParam param) {
        return driverOrderManagementMapper.getOrderList(param);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int cancelRelation(CancelRelationParam param) {
        SeleteOrderByIdVo vo = driverOrderManagementMapper.selectById(param.getOrderId());
        int rows = carServiceCollectMapper.updateCollect(vo);
        if(rows == 0){
            throw new ServiceException("取消关联失败");
        }
        return driverOrderManagementMapper.updateOrderById(param.getOrderId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int relation(RelationParam param) {
        String collectId = param.getCollectId();
        SeleteCarServiceCollectByIdVo collect = carServiceCollectMapper.getById(collectId);
        collect.setOrderId(param.getOrderId());
        SeleteAddressVo addrss = getAddrss(collect.getStartLocation(), collect.getEndLocation());
        collect.setStartSite(addrss.getStartSite());
        collect.setEndSite(addrss.getEndSite());
        int i = carServiceCollectMapper.updateCollectById(collectId);
        if(i == 0){
            throw new ServiceException("关联失败");
        }
        return driverOrderManagementMapper.updateOrder(collect);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SeleteAddressVo seleteAddress(SeleteAddressParam param) {
        SeleteAddressVo vo = new SeleteAddressVo();
        return getAddrss(param.getStartLocation(),param.getEndLocation());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<RelationCollectPageVo> relationCollectPage(RelationCollectPageParam param) {
        return carServiceCollectMapper.relationCollectPage(param);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GetDriverOrderEvaluateManagementVo getOrderEvaluateByOrderId(GetDriverOrderEvaluateManagementParam param) {
        return driverOrderManagementMapper.getOrderEvaluateByOrderId(param);
    }

    public SeleteAddressVo getAddrss(String startLocation,String endLocation) {
        SeleteAddressVo vo = new SeleteAddressVo();
        //获取调用百度地图接口密钥ak
        String ak = configService.selectConfigByKey("ak");
        if (!StringUtils.isEmpty(startLocation)) {
            //上车经纬度拼接
            String[] starts = startLocation.split(",");
            StringBuilder startBuilder = new StringBuilder();
            startBuilder.append(starts[1]);
            startBuilder.append(",");
            startBuilder.append(starts[0]);
            startLocation = startBuilder.toString();
            //调用百度地图接口
            String startSite = "";
            StringBuilder startUrl = new StringBuilder();
            startUrl.append("https://api.map.baidu.com/reverse_geocoding/v3/?ak=");
            startUrl.append(ak);
            startUrl.append("&output=json&coordtype=wgs84ll&extensions_town=true&location=");
            startUrl.append(startLocation);
            startSite = Http.doGet(startUrl.toString());
            if (startSite.equals("")) {
                startSite = Http.doGet(startUrl.toString());
            }
            vo.setStartSite(startSite);
        }
        if (!StringUtils.isEmpty(endLocation)) {
            //下车经纬度拼接
            String[] ends = endLocation.split(",");
            StringBuilder endBuilder = new StringBuilder();
            endBuilder.append(ends[1]);
            endBuilder.append(",");
            endBuilder.append(ends[0]);
            endLocation = endBuilder.toString();
            //调用百度地图接口
            String endSite = "";
            StringBuilder endUrl = new StringBuilder();
            endUrl.append("https://api.map.baidu.com/reverse_geocoding/v3/?ak=");
            endUrl.append(ak);
            endUrl.append("&output=json&coordtype=wgs84ll&extensions_town=true&location=");
            endUrl.append(endLocation);
            endSite = Http.doGet(endUrl.toString());
            if (endSite.equals("")) {
                endSite = Http.doGet(endUrl.toString());
            }
            vo.setEndSite(endSite);
        }
            return vo;
    }

}
